package photo.navi;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class CaptureLocationActivity extends Activity {
	static final int CAPTURE_IMAGE_REQUEST = 1;  // The request code
	static final String CAPTURE_DIRECTORY_NAME = "photonavi";
	static final String JPEG_FILE_EXTENSION = ".jpg";
	String current_file_path;
	Bitmap captured_photo;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_capture_location);
        
        try {
			takePhoto(null);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    }
    
    /** Called to create a temporary file and start Camera to capture image 
     * @throws IOException */
    public void takePhoto(View view) throws IOException {
    	Intent intent = new Intent( "android.media.action.IMAGE_CAPTURE");
    	current_file_path = createImagePath();
        startActivityForResult(intent, CAPTURE_IMAGE_REQUEST);
    }
    
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
        // Check which request we're responding to
        if (requestCode == CAPTURE_IMAGE_REQUEST) {
            // Make sure the request was successful
            if (resultCode == RESULT_OK) {
                // The user has taken a photo.
                // Display the photo in the view
            	Bundle extras = intent.getExtras();
                captured_photo = (Bitmap) extras.get("data");
                ImageView display = (ImageView) findViewById(R.id.capturedLocationView);
                display.setImageBitmap(captured_photo);
            }
        }
    }
    
    /** Returns full path to the directory where photos are stored */
    public File storageDir(){
    	File storage = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),CAPTURE_DIRECTORY_NAME);
    	storage.mkdirs();
    	return storage;
    }
    
    /** Returns a (possibly) unique filename that can be used for saving new captures*/
    public String uniqueFileName(){
    	return new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
    }
    

    private String createImagePath() throws IOException {
    	// Create an image file name
    	String imageFileName = uniqueFileName();
    	File image = File.createTempFile(imageFileName, JPEG_FILE_EXTENSION, storageDir());
    	//((TextView) findViewById(R.id.captureDescriptionEditText)).setHint(image.getAbsolutePath());
    	((TextView) findViewById(R.id.captureDescriptionEditText)).setHint(GpsManager.getInstance().getPosition());
    	String path = image.getAbsolutePath();
    	image.delete();
    	return path;
    }
    
    public void saveLocation(View view){
    	String location = current_file_path;
    	Bitmap bmp = captured_photo;
    	try {
	        FileOutputStream out = new FileOutputStream(location);
	        bmp.compress(Bitmap.CompressFormat.JPEG, 90, out);
    	} catch (Exception e) {
    		e.printStackTrace();
    	}
    	LocationDatabaseManager db = new LocationDatabaseManager(this);
    	EditText editText = (EditText) findViewById(R.id.captureDescriptionEditText);
    	String description = editText.getText().toString();
    	db.addPhotoLocation(new PhotoLocation(GpsManager.getInstance().getPosition(), current_file_path, description));
		Toast.makeText(view.getContext(), "The location has been saved", Toast.LENGTH_LONG).show();
    	try {
			takePhoto(view);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    }
}
